﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class RepairJob : _RepairJob
    {
        public RepairJob()
        {
        }

        public string BrandName
        {
            get { try { return this.GetColumn(View_RepairJobs.ColumnNames.BrandName).ToString(); } catch (Exception ex) { return ""; } }
            set { try { this.SetColumn(View_RepairJobs.ColumnNames.BrandName, value); } catch (Exception ex) { this.AddColumn(View_RepairJobs.ColumnNames.BrandName, typeof(string)); this.SetColumn(View_RepairJobs.ColumnNames.BrandName, value);} }
        }

        public string ModelName
        {
            get { try { return this.GetColumn(View_RepairJobs.ColumnNames.ModelName).ToString(); } catch (Exception ex) { return ""; } }
            set { try { this.SetColumn(View_RepairJobs.ColumnNames.ModelName, value); } catch (Exception ex) { this.AddColumn(View_RepairJobs.ColumnNames.ModelName, typeof(string)); this.SetColumn(View_RepairJobs.ColumnNames.ModelName, value);} }
        }

        public static int GetCustID(int jobID)
        {
            try
            {
                RepairJob job = new RepairJob();
                job.Where.ID.Value = jobID;
                job.Query.AddResultColumn(RepairJob.ColumnNames.CustomerID);
                job.Query.Load();

                return job.IsColumnNull(RepairJob.ColumnNames.CustomerID) ? -1 : job.CustomerID;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairJobProblems.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public RepairJob GetCurrentJobs()
        {
            try
            {
                RepairJob job = new RepairJob();
                
                job.Query.OpenParenthesis();
                job.Where.StatusID.Value = RepairStatus.Returned;
                job.Where.StatusID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;

                MyGeneration.dOOdads.WhereParameter whereStatusID = job.Where.TearOff.StatusID;
                whereStatusID.Conjuction = MyGeneration.dOOdads.WhereParameter.Conj.Or;
                whereStatusID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNull;
                job.Query.CloseParenthesis();

                job.Where.BranchID.Value = Branches.CurrentBranchID;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairJobProblems.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairJob GetCurrentRepairBoxByCustID(int custID)
        {
            try
            {
                RepairJob job = new RepairJob();
                
                job.Query.OpenParenthesis();
                job.Where.StatusID.Value = RepairStatus.Returned;
                job.Where.StatusID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;
                MyGeneration.dOOdads.WhereParameter whereStatusID = job.Where.TearOff.StatusID;
                whereStatusID.Conjuction = MyGeneration.dOOdads.WhereParameter.Conj.Or;
                whereStatusID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNull;
                job.Query.CloseParenthesis();
                job.Where.CustomerID.Value = custID;
                job.Where.RepairBoxID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNotNull;
                job.Query.Top = 1;
                job.Query.AddResultColumn(RepairJob.ColumnNames.RepairBoxID);
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairJobProblems.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairJob GetJobs(String str)
        {
            try
            {
                RepairJob job = new RepairJob();
                job.Where.ID.Value = str;
                job.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairJobProblems.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static string DrawMemo(string memo, string userName)
        {
            string rs = "<table><tr>";
            rs += string.Format("<td style='width:80px; text-align:left; vertical-align:top'>{0}</td>", DateTime.Now.ToShortDateString());
            rs += string.Format("<td style='width:250px; text-align:left; vertical-align:top'>{0}</td>", memo);
            rs += string.Format("<td style='width:100px; text-align:left; vertical-align:top'>{0}</td>", userName);
            rs += "</tr></table>";

            return rs;
        }

        public static RepairJob GetJobByInvoiceID(int invoiceID)
        {
            try
            {
                RepairJob job = new RepairJob();
                job.Where.InvoiceID.Value = invoiceID;
                job.Where.Deleted.Value = false;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairJobProblems.cs", "GetJobByInvoiceID", ex.StackTrace);
                throw ex;
            }
        }
    }
}
